package com.example.NeteaseCloudMusicApi.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.NeteaseCloudMusicApi.R;
import com.example.NeteaseCloudMusicApi.entity.FoundCircleDate;
import com.example.NeteaseCloudMusicApi.ui.activity.ListSongActivity;

import java.util.List;

public class FoundAdapter extends RecyclerView.Adapter<FoundAdapter.ViewHolder> {

    private List<FoundCircleDate> mFound;
    private Context context;
    private OnItemClickListener onItemClickListener;

    /**
     * 创建类ViewHolder（视图容器），承载的是条目中的控件
     * 做条目中控件的声明和绑定
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView name;

        public ViewHolder(View view) {
            super(view);
            image = view.findViewById(R.id.found_circle_button_image);
            name = view.findViewById(R.id.found_circle_button_information);
        }
    }

    /**
     * 创建构造函数
     * 构造函数参数是list的集合，他是rv直接的数据来源
     * 在做Adapter实例化的时候必须传入list的集合数据
     *
     * @param found
     */
    public FoundAdapter(Context context, List<FoundCircleDate> found) {
        this.context = context;
        this.mFound = found;
    }

    /**
     * 创建ViewHolder对象
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fount_circle_button, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    /**
     * 给ViewHolder中的控件设置数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        //加载图片和文字
        Glide.with(context).load(mFound.get(position).getIconUrl()).into(holder.image);
        holder.name.setText(mFound.get(position).getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ListSongActivity.class);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mFound.size();
    }

    //定义点击接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    //点击方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;

    }
}
